﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace mp.message.util
{
    public class CommonFun
    {
        public static void PrintLog(string errPos,string errMsg)
        {
            StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath("weixin.log"), true);
            sw.WriteLine(DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToShortTimeString()+":"+errPos+"--"+errMsg);
            sw.Flush();
            sw.Close();
        }

        public static void PrintExceptionLog(Exception ex)
        {
            PrintLog("---------------产生异常", "-----------------");
            PrintLog("Message --{0}", ex.Message);
            PrintLog("HelpLink --{0}", ex.HelpLink);
            PrintLog("Source --{0}", ex.Source);
            PrintLog("StackTrace --{0}", ex.StackTrace);
            PrintLog("TargetSite --{0}", ex.TargetSite.ToString());
            PrintLog("---------------异常结束", "-----------------");
        }

        public static DateTime CreateTime(string createTime)
        {
            DateTime init = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            try
            {
                long timeTick = Convert.ToInt32(createTime)+(8*3600);
                init = init.AddTicks(timeTick * 10000000);
            }
            catch (Exception ex)
            {
                CommonFun.PrintExceptionLog(ex);
            }
            return init;
        }
    }

        /// <summary>
    /// 得到从微信POST过来的XML字符串信息
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    //public string GetPostXmlString(HttpContext context)
    //{
    //    string postStr = "";
    //    if (context.Request.HttpMethod.ToLower() == "post")//当普通微信用户向公众账号发消息时，微信服务器将POST该消息到填写的URL上
    //    {
    //        System.IO.Stream s = context.Request.InputStream;
    //        byte[] b = new byte[s.Length];
    //        s.Read(b, 0, (int)s.Length);
    //        postStr = System.Text.Encoding.UTF8.GetString(b);//得到微信POST过来的XML格式数据。
    //        if (string.IsNullOrEmpty(postStr) == false)
    //        {
    //            return postStr;   
    //        }
    //    }
    //    return postStr;
    //}
}
